/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;
import org.aswing.util.HashMap;

/**
 * <strong>FvContextMenuUtils</strong> provides control over the 
 * <strong>AsWing components</strong> in the Flash Player context menu.
 * 
 * <p>You can use the methods and properties of the Flash ContextMenu class to 
 * add custom menu items, control the display of the built-in context menu 
 * items.
 * 
 * <p>Example
 * {@code
 *	public function test() : Void
 *	{
 *		var m : ContextMenu = new ContextMenu();
 *		m.customItems.push( new ContextMenuItem( "submenuA", _proxy, false, true ) );
 *		m.customItems.push( new ContextMenuItem( "submenuB", _proxy, false, true ) );
 *		m.customItems.push( new ContextMenuItem( "submenuC", _proxy, false, false ) );
 *		m.hideBuiltInItems();
 *		
 *		var button : Jbutton = new JButton( "test" );
 *		
 *		FvContextMenuUtils.applyContextMenu( button, m );
 *	}
 * }
 * @author Romain Ecarnot
 */
class fvaswing.utils.FvContextMenuUtils
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _storeMap : HashMap = new HashMap();
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
		
	/**
	 * Apply a ContextMenu to a component.
	 * 
	 * <p>Component must be created before any menu definition.<br>
	 * Listen to {@code Component.ON_CREATED} event.
	 * 
	 * @param comp target Component
	 * @param contextMenu a ContextMenu instance
	 */
	public static function applyContextMenu( comp : Component, contextMenu : ContextMenu ) : Void
	{
		var target : MovieClip = comp.createMovieClipOnRoot( "__context__" );
		var menuTarget : MovieClip = target._parent;
		target.removeMovieClip();
		menuTarget.menu = contextMenu;
		
		_storeMap.put( comp.getID(), contextMenu );
	}
	
	/**
	 * Returns the ContextMenu instance associated with passed-in component.
	 * 
	 * @param comp The component source
	 * @return A ContextMenu instance ( or {@code null }
	 */
	public static function getContextMenu( comp : Component ) : ContextMenu
	{
		if( comp )
		{
			return _storeMap.get( comp.getID() );
		}
		else return null;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function FvContextMenuUtils()
	{
		
	}	
}